const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('buy_medicine', {
    id: {
      autoIncrement: true,
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    amount: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "数量"
    },
    state: {
      type: DataTypes.ENUM('等待护士打包','等待患者取药','交易关闭','交易失败'),
      allowNull: false,
      defaultValue: "等待护士打包",
      comment: "状态"
    },
    memo: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: "护士备注"
    },
    nurse_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      }
    },
    patient_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: true,
      references: {
        model: 'patient',
        key: 'id'
      }
    },
    medicine_lib_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: true,
      references: {
        model: 'medicine_lib',
        key: 'id'
      }
    }
  }, {
    sequelize,
    tableName: 'buy_medicine',
    timestamps: true,
    indexes: [
      {
        name: "PRIMARY",
        unique: true,
        using: "BTREE",
        fields: [
          { name: "id" },
        ]
      },
      {
        name: "nurse_id",
        using: "BTREE",
        fields: [
          { name: "nurse_id" },
        ]
      },
      {
        name: "patient_id",
        using: "BTREE",
        fields: [
          { name: "patient_id" },
        ]
      },
      {
        name: "medicine_lib_id",
        using: "BTREE",
        fields: [
          { name: "medicine_lib_id" },
        ]
      },
    ]
  });
};
